package print;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JTable;

import tournamentData.TournamentData;
import core.CustomIcons;

public class TablePrinter implements Printable {
	private final Printable tablePrintable;
	private PageFormat pageFormatJTable;
	private final String subTitle;
	private final int headerspace = 60;
	private final int footerspace = 90;
	private final int border = 15;

	public TablePrinter(final JTable table, String subTitle) {
		this.tablePrintable = table.getPrintable(JTable.PrintMode.FIT_WIDTH, null, null);
		this.subTitle = subTitle;
	}

	public int print(final Graphics graphics, final PageFormat pageFormat, final int pageIndex) throws PrinterException {
		/**
		 * set up the page
		 */

		Graphics2D graphics2D = (Graphics2D) graphics;
		int leftmostImageable = (int) pageFormat.getImageableX();
		int topmostImageable = (int) pageFormat.getImageableY();
		int imageableWidth = (int) pageFormat.getImageableWidth();
		int imageableHeight = (int) pageFormat.getImageableHeight();
		if (this.pageFormatJTable == null) {
			this.pageFormatJTable = (PageFormat) pageFormat.clone();
			Paper paperJTable = this.pageFormatJTable.getPaper();
			if (this.pageFormatJTable.getOrientation() == PageFormat.PORTRAIT) {
				paperJTable.setImageableArea(leftmostImageable,
				/* skip header area */
				topmostImageable + this.headerspace, imageableWidth,
				/* reserve space for header and footer */
				imageableHeight - this.footerspace);
			} else {
				paperJTable.setImageableArea(
				/* skip header area */
				topmostImageable + this.headerspace, leftmostImageable,
				/* reserve space for header and footer */
				imageableHeight - this.footerspace, imageableWidth);
			}
			this.pageFormatJTable.setPaper(paperJTable);
		}

		Font font = graphics2D.getFont();

		/**
		 * set up title
		 */

		String title = TournamentData.getInstance().getGeneralTournamentInformation().getTournamentName();
		title += " (" + TournamentData.getInstance().getGeneralTournamentInformation().getStartDate() + ")";
		/* increase size of header */
		graphics2D.setFont(graphics2D.getFont().deriveFont(15f));
		FontMetrics fontMetrics = graphics2D.getFontMetrics();
		graphics2D.drawString(title, leftmostImageable, topmostImageable + this.border);
		// graphics2D.drawString(title, rightmostImageable + ((imageableWidth -
		// fontMetrics.stringWidth(title)) / 2), topmostImageable + 15);

		/**
		 * set up subtitle
		 */

		String subtitle = this.subTitle;
		graphics2D.setFont(font);
		fontMetrics = graphics2D.getFontMetrics();
		graphics2D.drawString(subtitle, leftmostImageable, topmostImageable + 30);

		/**
		 * set up ArComp logo
		 */
		int logoWidth = 100;
		int logoHeight = 50;
		graphics2D.drawImage(CustomIcons.logoImage, (leftmostImageable + imageableWidth) - logoWidth, topmostImageable, logoWidth, logoHeight, Color.WHITE, null);
		// graphics2D.drawRect(leftmostImageable, topmostImageable,
		// imageableWidth, 40);

		/**
		 * set up footer
		 */
		String pageNumber = "Seite " + (pageIndex + 1);
		graphics2D.drawString(pageNumber, leftmostImageable, (topmostImageable + imageableHeight));

		// add printing date
		String DATE_FORMAT_NOW = "dd.MM.yyyy, HH:mm:ss";
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		String date = sdf.format(cal.getTime());
		graphics2D.drawString(date, (leftmostImageable + imageableWidth) - fontMetrics.stringWidth(date), (topmostImageable + imageableHeight));

		/**
		 * insert the table:
		 */
		Graphics gCopy = graphics2D.create();
		// System.out.println("before: " + pageIndex);
		int retVal = this.tablePrintable.print(gCopy, this.pageFormatJTable, pageIndex);
		// System.out.println("after: " + pageIndex);
		gCopy.dispose();

		return retVal;
	}
}
